<?php
require_once("Accountant.class.php");
require_once("exceptions/AppExceptions.class.php");

class MonitorFileAccountant extends Accountant {

	/**
	 * 文件Id
	 *
	 * @var int
	 */
	private $fileId = null;
	
	
	private $certificationNumber = array();
	
	private $facheNumber = array();
	
	private $certificationCodification = array();
	
	private $sequenceNumber = array();
	 
	private $manufactory = array();
	
	private $authenticationMark = array();
	
	private $steelNumber =  array();
	
	private $size1 = array();
	
	private $size2 = array();
	
	private $size3 = array();
	
	private $count = array();
	
	private $unitWeight = array();
	
	private $orderNumber = array();
	
	private $orderSubitemNumber = array();
	
	private $unitPrice = array();
	
	private $materialNumber = array();
	
	private $consignmentBatch = array();
	
	private $sampleBatch = array();
	
	private $purchaseNumber = array();
	
	private $shipmentDate = array();
	
	private $month = array();
	
	private $vehicleNumber = array();
	
	private $consignee = array();
	
	private $remark = array();
	
	private $stat = array();

	private $deleteSql = array();
	
	
	//以下成员变量是对未能在总表中找到对应数据的监控条目
	
	private $notInMainSql;
	
	private $notInMainCertificationNumber = array();
	
	private $notInMainFacheNumber = array();
	
	private $notInMainCertificationCodification = array();
	
	private $notInMainSequenceNumber = array();
	 
	private $notInMainManufactory = array();
	
	private $notInMainAuthenticationMark = array();
	
	private $notInMainSteelNumber =  array();
	
	private $notInMainSize1 = array();
	
	private $notInMainSize2 = array();
	
	private $notInMainSize3 = array();
	
	private $notInMainCount = array();
	
	private $notInMainUnitWeight = array();
	
	private $notInMainOrderNumber = array();
	
	private $notInMainOrderSubitemNumber = array();
	
	private $notInMainUnitPrice = array();
	
	private $notInMainMaterialNumber = array();
	
	private $notInMainSampleBatch = array();
	
	private $notInMainPurchaseNumber = array();
	
	private $notInMainShipmentDate = array();
	
	private $notInMainMonth = array();
	
	private $notInMainVehicleNumber = array();
	
	private $notInMainConsignee = array();
	
	private $notInMainRemark = array();
	
	/**
	 * 标志这个监控文件的数据中是否有mc
	 *
	 * @var bool
	 */
	private $haveCn = false;
	/**
	 * 构造函数，根据文件名称初始化sql语句。
	 *
	 * @param string $filename
	 */
	public function __construct($fileId){
		
		$query = "select * from little_monitor where fileId = '{$fileId}'";
		DB::query($query);
		$result = DB::getResult();
		
		$tableCols = array(
				'产品质量证明书号' 	=> 	'certificationNumber',
				'发车证书号'	=> 'facheNumber',
				'产品质量证明书编号' 		=> 	'certificationCodification',
				'批次' 		=> 	'sequenceNumber',
				'生产厂家' 		=> 	'manufactory',
				'船级' 		=> 	'authenticationMark',
				'材质' 		=> 	'steelNumber',
				'厚' 		=> 	'size1',
				'宽' 	=> 	'size2',
				'长' 	=> 	'size3',
				'数量'	=>	'count',
				'单重'		=>	'unitWeight',
				'订单号'		=>	'orderNumber',
				'订单子项号'		=>	'orderSubitemNumber',
				'受订单价'		=>	'unitPrice',
				'物料号'		=>  'materialNumber',
				'发货批次'	=>  'consignmentBatch',
				'批号'		=>  'sampleBatch',
				'购单号'	=>  'purchaseNumber',
				'发货时间'	=>  'shipmentDate',
				'合同月份'	=>  'month',
				'车号'	=>  'vehicleNumber',
				'销售产家'	=>  'consignee',
				'备注'	=>  'remark'
		);
		$cols = array();
		if($row = $result->fetch_assoc()){
			foreach($tableCols as $key => $val){
				if(isset($row[$val]) and !empty($row[$val])){
					array_push($cols, $val);
				}
			}
		}
		
		$this->haveCn = false;
		if(in_array('certificationNumber', $cols)){
			$colswhere = 'certificationNumber';
			$this->haveCn = true;
		}
		
		$colswhere = join(',', $cols);
		
		if(empty($colswhere)){
			$this->sql = null;
			return;
		}
		
		//如果有count怎么办？
		//如果没有count又该怎么办？
		
		$this->sql = "select * from little_main
			where ($colswhere) in (select $colswhere from little_monitor where fileId = $fileId)";
		
		$this->notInMainSql = "select * from little_monitor where fileId = $fileId
		and ($colswhere) not in (select $colswhere from little_main)";
		
		///////////////////////////////////////////////

			array_push($this->deleteSql,
			"delete from little_ruku 
			where exists (select 1
					from little_main 
					where ($colswhere) in (select $colswhere from little_monitor where fileId = $fileId)
						and little_main.certificationNumber = little_ruku.certificationNumber)");
			
			array_push($this->deleteSql,
			"delete from little_chuku 
			where exists (select 1
					from little_main 
					where ($colswhere) in (select $colswhere from little_monitor where fileId = $fileId)
						and little_main.certificationNumber = little_chuku.certificationNumber)");
			
			array_push($this->deleteSql,
			"delete from little_main where
			($colswhere) in (select $colswhere from little_monitor where fileId = $fileId)");
			
	}
	
	public function executeDelete(){
		
		try{
			DB::beginTransaction();
			foreach($this->deleteSql as $val){
				DB::query($val);
			}
			DB::commit();
		}catch(Exception $e){
			DB::rollback();
			throw $e;
		}
		
	}

	/**
	 * 执行查询命令，并整理查询的结果
	 *
	 */
	public function execute(){
		$this->result = DB::query($this->sql);
		if($this->result == false){
			return;
		}
		while($row = $this->result->fetch_assoc()){
			array_push($this->certificationNumber, $row['certificationNumber']);
			array_push($this->facheNumber, $row['facheNumber']);
			array_push($this->certificationCodification, $row['certificationCodification']);
			array_push($this->sequenceNumber, $row['sequenceNumber']);
			array_push($this->manufactory, $row['manufactory']);
			array_push($this->authenticationMark, $row['authenticationMark']);
			array_push($this->steelNumber, $row['steelNumber']);	
			array_push($this->size1, $row['size1']);
			array_push($this->size2, $row['size2']);
			array_push($this->size3, $row['size3']);
			array_push($this->unitWeight, $row['unitWeight']);		
			array_push($this->count, $row['count']);
			array_push($this->orderNumber, $row['orderNumber']);
			array_push($this->orderSubitemNumber, $row['orderSubitemNumber']);
			array_push($this->unitPrice,$row['unitPrice']);
			array_push($this->materialNumber, $row['materialNumber']);
			array_push($this->consignmentBatch, $row['consignmentBatch']);
			array_push($this->sampleBatch,$row['sampleBatch']);
			array_push($this->purchaseNumber,$row['purchaseNumber']);
			array_push($this->shipmentDate,$row['shipmentDate']);
			array_push($this->month,$row['month']);
			array_push($this->vehicleNumber, $row['vehicleNumber']);
			array_push($this->consignee, $row['consignee']);
			array_push($this->remark, $row['remark']);
			$query_chuku = "select certificationNumber from little_chuku where certificationNumber = '{$row['certificationNumber']}'";
			DB::query($query_chuku);
			$row_chuku = DB::getResult()->fetch_assoc();
			if(!empty($row['certificationNumber']) && isset($row['certificationNumber'])){
				if(count($row_chuku['certificationNumber'])>0){
					array_push($this->stat, '出库');
				}else{
					$query_ruku = "select certificationNumber from little_ruku 
					where certificationNumber = '{$row['certificationNumber']}'";
					DB::query($query_ruku);
					$row_ruku = DB::getResult()->fetch_assoc();
					if(count($row_ruku['certificationNumber'])>0){
						array_push($this->stat,'库中');
					}else{
						array_push($this->stat,'未动');
					}
				}
			}else 
				array_push($this->stat,'');
		}
		
		
	$this->result = DB::query($this->notInMainSql);
		if($this->result == false){
			return;
		}
		while(($row = $this->result->fetch_assoc())!=null){
			array_push($this->notInMainCertificationNumber, $row['certificationNumber']);
			array_push($this->notInMainFacheNumber, $row['facheNumber']);
			array_push($this->notInMainCertificationCodification, $row['certificationCodification']);
			array_push($this->notInMainSequenceNumber, $row['sequenceNumber']);
			array_push($this->notInMainManufactory, $row['manufactory']);
			array_push($this->notInMainAuthenticationMark, $row['authenticationMark']);
			array_push($this->notInMainSteelNumber, $row['steelNumber']);	
			array_push($this->notInMainSize1, $row['size1']);
			array_push($this->notInMainSize2, $row['size2']);
			array_push($this->notInMainSize3, $row['size3']);
			array_push($this->notInMainUnitWeight, $row['unitWeight']);		
			array_push($this->notInMainCount, $row['count']);
			array_push($this->notInMainOrderNumber, $row['orderNumber']);
			array_push($this->notInMainOrderSubitemNumber, $row['orderSubitemNumber']);
			array_push($this->notInMainUnitPrice,$row['unitPrice']);
			array_push($this->notInMainMaterialNumber, $row['materialNumber']);
			array_push($this->notInMainSampleBatch,$row['sampleBatch']);
			array_push($this->notInMainPurchaseNumber,$row['purchaseNumber']);
			array_push($this->notInMainShipmentDate,$row['shipmentDate']);
			array_push($this->notInMainMonth,$row['month']);
			array_push($this->notInMainVehicleNumber, $row['vehicleNumber']);
			array_push($this->notInMainConsignee, $row['consignee']);
		}
	}
	/**
	 * @return the $haveCn
	 */
	public function getHaveCn() {
		return $this->haveCn;
	}

	/**
	 * @return the $notInMainRemark
	 */
	public function getNotInMainRemark() {
		return $this->notInMainRemark;
	}

	/**
	 * @return the $notInMainConsignee
	 */
	public function getNotInMainConsignee() {
		return $this->notInMainConsignee;
	}

	/**
	 * @return the $notInMainVehicleNumber
	 */
	public function getNotInMainVehicleNumber() {
		return $this->notInMainVehicleNumber;
	}

	/**
	 * @return the $notInMainMonth
	 */
	public function getNotInMainMonth() {
		return $this->notInMainMonth;
	}

	/**
	 * @return the $notInMainShipmentDate
	 */
	public function getNotInMainShipmentDate() {
		return $this->notInMainShipmentDate;
	}

	/**
	 * @return the $notInMainPurchaseNumber
	 */
	public function getNotInMainPurchaseNumber() {
		return $this->notInMainPurchaseNumber;
	}

	/**
	 * @return the $notInMainSampleBatch
	 */
	public function getNotInMainSampleBatch() {
		return $this->notInMainSampleBatch;
	}

	/**
	 * @return the $notInMainMaterialNumber
	 */
	public function getNotInMainMaterialNumber() {
		return $this->notInMainMaterialNumber;
	}

	/**
	 * @return the $notInMainUnitPrice
	 */
	public function getNotInMainUnitPrice() {
		return $this->notInMainUnitPrice;
	}

	/**
	 * @return the $notInMainOrderSubitemNumber
	 */
	public function getNotInMainOrderSubitemNumber() {
		return $this->notInMainOrderSubitemNumber;
	}

	/**
	 * @return the $notInMainOrderNumber
	 */
	public function getNotInMainOrderNumber() {
		return $this->notInMainOrderNumber;
	}

	/**
	 * @return the $notInMainUnitWeight
	 */
	public function getNotInMainUnitWeight() {
		return $this->notInMainUnitWeight;
	}

	/**
	 * @return the $notInMainCount
	 */
	public function getNotInMainCount() {
		return $this->notInMainCount;
	}

	/**
	 * @return the $notInMainSize3
	 */
	public function getNotInMainSize3() {
		return $this->notInMainSize3;
	}

	/**
	 * @return the $notInMainSize2
	 */
	public function getNotInMainSize2() {
		return $this->notInMainSize2;
	}

	/**
	 * @return the $notInMainSize1
	 */
	public function getNotInMainSize1() {
		return $this->notInMainSize1;
	}

	/**
	 * @return the $notInMainSteelNumber
	 */
	public function getNotInMainSteelNumber() {
		return $this->notInMainSteelNumber;
	}

	/**
	 * @return the $notInMainAuthenticationMark
	 */
	public function getNotInMainAuthenticationMark() {
		return $this->notInMainAuthenticationMark;
	}

	/**
	 * @return the $notInMainManufactory
	 */
	public function getNotInMainManufactory() {
		return $this->notInMainManufactory;
	}

	/**
	 * @return the $notInMainSequenceNumber
	 */
	public function getNotInMainSequenceNumber() {
		return $this->notInMainSequenceNumber;
	}

	/**
	 * @return the $notInMainCertificationCodification
	 */
	public function getNotInMainCertificationCodification() {
		return $this->notInMainCertificationCodification;
	}

	/**
	 * @return the $notInMainFacheNumber
	 */
	public function getNotInMainFacheNumber() {
		return $this->notInMainFacheNumber;
	}

	/**
	 * @return the $notInMainCertificationNumber
	 */
	public function getNotInMainCertificationNumber() {
		return $this->notInMainCertificationNumber;
	}

	/**
	 * @return the $notInMainSql
	 */
	public function getNotInMainSql() {
		return $this->notInMainSql;
	}


	/**
	 * @return unknown
	 */
	public function getAuthenticationMark() {
		return $this->authenticationMark;
	}
	
	/**
	 * @return unknown
	 */
	public function getCertificationCodification() {
		return $this->certificationCodification;
	}
	
	/**
	 * @return unknown
	 */
	public function getCertificationNumber() {
		return $this->certificationNumber;
	}
	
	public function getFacheNumber(){
		return $this->facheNumber;
	}
	
	public function getStat(){
		return $this->stat;
	}
	
	/**
	 * @return unknown
	 */
	public function getConsignee() {
		return $this->consignee;
	}
	
	/**
	 * @return unknown
	 */
	public function getConsignmentBatch() {
		return $this->consignmentBatch;
	}
	
	/**
	 * @return unknown
	 */
	public function getCount() {
		return $this->count;
	}
	
	/**
	 * @return int
	 */
	public function getFileId() {
		return $this->fileId;
	}
	
	/**
	 * @return unknown
	 */
	public function getHaveMc() {
		return $this->haveCn;
	}
	
	/**
	 * @return unknown
	 */
	public function getManufactory() {
		return $this->manufactory;
	}
	
	/**
	 * @return unknown
	 */
	public function getMaterialNumber() {
		return $this->materialNumber;
	}
	
	/**
	 * @return unknown
	 */
	public function getMonth() {
		return $this->month;
	}
	
	/**
	 * @return unknown
	 */
	public function getOrderNumber() {
		return $this->orderNumber;
	}
	
	/**
	 * @return unknown
	 */
	public function getOrderSubitemNumber() {
		return $this->orderSubitemNumber;
	}
	
	/**
	 * @return unknown
	 */
	public function getPurchaseNumber() {
		return $this->purchaseNumber;
	}
	
	/**
	 * @return unknown
	 */
	public function getRemark() {
		return $this->remark;
	}
	
	/**
	 * @return unknown
	 */
	public function getSampleBatch() {
		return $this->sampleBatch;
	}
	
	/**
	 * @return unknown
	 */
	public function getSequenceNumber() {
		return $this->sequenceNumber;
	}
	
	/**
	 * @return unknown
	 */
	public function getShipmentDate() {
		return $this->shipmentDate;
	}
	
	/**
	 * @return unknown
	 */
	public function getSize1() {
		return $this->size1;
	}
	
	/**
	 * @return unknown
	 */
	public function getSize2() {
		return $this->size2;
	}
	
	/**
	 * @return unknown
	 */
	public function getSize3() {
		return $this->size3;
	}
	
	/**
	 * @return unknown
	 */
	public function getSteelNumber() {
		return $this->steelNumber;
	}
	
	/**
	 * @return unknown
	 */
	public function getUnitPrice() {
		return $this->unitPrice;
	}
	
	/**
	 * @return unknown
	 */
	public function getUnitWeight() {
		return $this->unitWeight;
	}
	
	/**
	 * @return unknown
	 */
	public function getVehicleNumber() {
		return $this->vehicleNumber;
	}
}
?>